<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.savetxt &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Input and output" href="../routines.io.html" >
    <link rel="next" title="numpy.genfromtxt" href="numpy.genfromtxt.html" >
    <link rel="prev" title="numpy.savez_compressed" href="numpy.savez_compressed.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.io.html" accesskey="U">Input and output</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.genfromtxt.html" title="numpy.genfromtxt"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.savez_compressed.html" title="numpy.savez_compressed"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.savez_compressed.html"
                        title="previous chapter">numpy.savez_compressed</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.genfromtxt.html"
                        title="next chapter">numpy.genfromtxt</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-savetxt">
<h1>numpy.savetxt<a class="headerlink" href="#numpy-savetxt" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="numpy.savetxt">
<code class="sig-prename descclassname">numpy.</code><code class="sig-name descname">savetxt</code><span class="sig-paren">(</span><em class="sig-param">fname</em>, <em class="sig-param">X</em>, <em class="sig-param">fmt='%.18e'</em>, <em class="sig-param">delimiter=' '</em>, <em class="sig-param">newline='n'</em>, <em class="sig-param">header=''</em>, <em class="sig-param">footer=''</em>, <em class="sig-param">comments='# '</em>, <em class="sig-param">encoding=None</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/lib/npyio.py#L1211-L1459"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.savetxt" title="Permalink to this definition">¶</a></dt>
<dd><p>Save an array to a text file.</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>fname</strong><span class="classifier">filename or file handle</span></dt><dd><p>If the filename ends in <code class="docutils literal notranslate"><span class="pre">.gz</span></code>, the file is automatically saved in
compressed gzip format.  <a class="reference internal" href="numpy.loadtxt.html#numpy.loadtxt" title="numpy.loadtxt"><code class="xref py py-obj docutils literal notranslate"><span class="pre">loadtxt</span></code></a> understands gzipped files
transparently.</p>
</dd>
<dt><strong>X</strong><span class="classifier">1D or 2D array_like</span></dt><dd><p>Data to be saved to a text file.</p>
</dd>
<dt><strong>fmt</strong><span class="classifier">str or sequence of strs, optional</span></dt><dd><p>A single format (%10.5f), a sequence of formats, or a
multi-format string, e.g. ‘Iteration %d – %10.5f’, in which
case <em class="xref py py-obj">delimiter</em> is ignored. For complex <em class="xref py py-obj">X</em>, the legal options
for <em class="xref py py-obj">fmt</em> are:</p>
<ul class="simple">
<li><p>a single specifier, <em class="xref py py-obj">fmt=’%.4e’</em>, resulting in numbers formatted
like <em class="xref py py-obj">‘ (%s+%sj)’ % (fmt, fmt)</em></p></li>
<li><p>a full string specifying every real and imaginary part, e.g.
<em class="xref py py-obj">‘ %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej’</em> for 3 columns</p></li>
<li><p>a list of specifiers, one per column - in this case, the real
and imaginary part must have separate specifiers,
e.g. <em class="xref py py-obj">[‘%.3e + %.3ej’, ‘(%.15e%+.15ej)’]</em> for 2 columns</p></li>
</ul>
</dd>
<dt><strong>delimiter</strong><span class="classifier">str, optional</span></dt><dd><p>String or character separating columns.</p>
</dd>
<dt><strong>newline</strong><span class="classifier">str, optional</span></dt><dd><p>String or character separating lines.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.5.0.</span></p>
</div>
</dd>
<dt><strong>header</strong><span class="classifier">str, optional</span></dt><dd><p>String that will be written at the beginning of the file.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.7.0.</span></p>
</div>
</dd>
<dt><strong>footer</strong><span class="classifier">str, optional</span></dt><dd><p>String that will be written at the end of the file.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.7.0.</span></p>
</div>
</dd>
<dt><strong>comments</strong><span class="classifier">str, optional</span></dt><dd><p>String that will be prepended to the <code class="docutils literal notranslate"><span class="pre">header</span></code> and <code class="docutils literal notranslate"><span class="pre">footer</span></code> strings,
to mark them as comments. Default: ‘# ‘,  as expected by e.g.
<code class="docutils literal notranslate"><span class="pre">numpy.loadtxt</span></code>.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.7.0.</span></p>
</div>
</dd>
<dt><strong>encoding</strong><span class="classifier">{None, str}, optional</span></dt><dd><p>Encoding used to encode the outputfile. Does not apply to output
streams. If the encoding is something other than ‘bytes’ or ‘latin1’
you will not be able to load the file in NumPy versions &lt; 1.14. Default
is ‘latin1’.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 1.14.0.</span></p>
</div>
</dd>
</dl>
</dd>
</dl>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt><a class="reference internal" href="numpy.save.html#numpy.save" title="numpy.save"><code class="xref py py-obj docutils literal notranslate"><span class="pre">save</span></code></a></dt><dd><p>Save an array to a binary file in NumPy <code class="docutils literal notranslate"><span class="pre">.npy</span></code> format</p>
</dd>
<dt><a class="reference internal" href="numpy.savez.html#numpy.savez" title="numpy.savez"><code class="xref py py-obj docutils literal notranslate"><span class="pre">savez</span></code></a></dt><dd><p>Save several arrays into an uncompressed <code class="docutils literal notranslate"><span class="pre">.npz</span></code> archive</p>
</dd>
<dt><a class="reference internal" href="numpy.savez_compressed.html#numpy.savez_compressed" title="numpy.savez_compressed"><code class="xref py py-obj docutils literal notranslate"><span class="pre">savez_compressed</span></code></a></dt><dd><p>Save several arrays into a compressed <code class="docutils literal notranslate"><span class="pre">.npz</span></code> archive</p>
</dd>
</dl>
</div>
<p class="rubric">Notes</p>
<p>Further explanation of the <em class="xref py py-obj">fmt</em> parameter
(<code class="docutils literal notranslate"><span class="pre">%[flag]width[.precision]specifier</span></code>):</p>
<dl>
<dt>flags:</dt><dd><p><code class="docutils literal notranslate"><span class="pre">-</span></code> : left justify</p>
<p><code class="docutils literal notranslate"><span class="pre">+</span></code> : Forces to precede result with + or -.</p>
<p><code class="docutils literal notranslate"><span class="pre">0</span></code> : Left pad the number with zeros instead of space (see width).</p>
</dd>
<dt>width:</dt><dd><p>Minimum number of characters to be printed. The value is not truncated
if it has more characters.</p>
</dd>
<dt>precision:</dt><dd><ul class="simple">
<li><p>For integer specifiers (eg. <code class="docutils literal notranslate"><span class="pre">d,i,o,x</span></code>), the minimum number of
digits.</p></li>
<li><p>For <code class="docutils literal notranslate"><span class="pre">e,</span> <span class="pre">E</span></code> and <code class="docutils literal notranslate"><span class="pre">f</span></code> specifiers, the number of digits to print
after the decimal point.</p></li>
<li><p>For <code class="docutils literal notranslate"><span class="pre">g</span></code> and <code class="docutils literal notranslate"><span class="pre">G</span></code>, the maximum number of significant digits.</p></li>
<li><p>For <code class="docutils literal notranslate"><span class="pre">s</span></code>, the maximum number of characters.</p></li>
</ul>
</dd>
<dt>specifiers:</dt><dd><p><code class="docutils literal notranslate"><span class="pre">c</span></code> : character</p>
<p><code class="docutils literal notranslate"><span class="pre">d</span></code> or <code class="docutils literal notranslate"><span class="pre">i</span></code> : signed decimal integer</p>
<p><code class="docutils literal notranslate"><span class="pre">e</span></code> or <code class="docutils literal notranslate"><span class="pre">E</span></code> : scientific notation with <code class="docutils literal notranslate"><span class="pre">e</span></code> or <code class="docutils literal notranslate"><span class="pre">E</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">f</span></code> : decimal floating point</p>
<p><code class="docutils literal notranslate"><span class="pre">g,G</span></code> : use the shorter of <code class="docutils literal notranslate"><span class="pre">e,E</span></code> or <code class="docutils literal notranslate"><span class="pre">f</span></code></p>
<p><code class="docutils literal notranslate"><span class="pre">o</span></code> : signed octal</p>
<p><code class="docutils literal notranslate"><span class="pre">s</span></code> : string of characters</p>
<p><code class="docutils literal notranslate"><span class="pre">u</span></code> : unsigned decimal integer</p>
<p><code class="docutils literal notranslate"><span class="pre">x,X</span></code> : unsigned hexadecimal integer</p>
</dd>
</dl>
<p>This explanation of <code class="docutils literal notranslate"><span class="pre">fmt</span></code> is not complete, for an exhaustive
specification see <a class="reference internal" href="#r672d4d5b6143-1" id="id1">[1]</a>.</p>
<p class="rubric">References</p>
<dl class="citation">
<dt class="label" id="r672d4d5b6143-1"><span class="brackets"><a class="fn-backref" href="#id1">1</a></span></dt>
<dd><p><a class="reference external" href="https://docs.python.org/library/string.html#format-specification-mini-language">Format Specification Mini-Language</a>,
Python Documentation.</p>
</dd>
</dl>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">y</span> <span class="o">=</span> <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span><span class="mf">5.0</span><span class="p">,</span><span class="mf">1.0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">savetxt</span><span class="p">(</span><span class="s1">&#39;test.out&#39;</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">delimiter</span><span class="o">=</span><span class="s1">&#39;,&#39;</span><span class="p">)</span>   <span class="c1"># X is an array</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">savetxt</span><span class="p">(</span><span class="s1">&#39;test.out&#39;</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span><span class="p">))</span>   <span class="c1"># x,y,z equal sized 1D arrays</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">savetxt</span><span class="p">(</span><span class="s1">&#39;test.out&#39;</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">fmt</span><span class="o">=</span><span class="s1">&#39;</span><span class="si">%1.4e</span><span class="s1">&#39;</span><span class="p">)</span>   <span class="c1"># use exponential notation</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>